#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# 生成器有些类似js中的生成器，只不过python的定义方式和js中不一样而已
def gen():
    yield 1
    yield 2
    return 3

g = gen()
print(next(g)) # 1
print(next(g)) # 2
# 不能直接拿到return中的值
# print(next(g)) # Error:StopIteration: 3
# return中的值包含在StopIteration对象的value中
try:
    next(g)
except StopIteration as e:
    print(e.value)

for a in gen():
    print(a) # 1 2
